Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move header value normalisation into Response #81

Closed

Conversation

thgs
Copy link

@thgs thgs commented Dec 9, 2023

Q A
Documentation no
Bugfix no
BC Break no
New Feature no
RFC yes
QA yes/no

Description

When not using laminas-http as the HTTP client the implementation of the HeaderAwareClientInterface one may have to reproduce the logic to normalise headers from multi value to string as it is done in here.

I am partial about whether what I am doing here is right (or desired) or wrong (will have problems), so it is more of a question, in the form of a PR for clarity.

The benefit of this change is that when implementing the interfaces needed to inject a third party client, one does not need to deal with the case of multi value headers and can reuse the Response class provided (Laminas\Feed\Reader\Http\Response), therefore only one implementation is needed implementing HeaderAwareClientInterface.

I am relatively new to the codebase so let me know if I have missed some profound/foundational concern here. I can see a slight violation of single responsibility principle here.

On the other hand, it is a (micro?) optimisation as the loop through the headers only happens once.

Lastly, given this is desired, maybe a unit test case should be added as well as an addition in the docs explaining the above "convenience". Ofc I am willing to do these and any other changes requested but I thought it would be nice if first it is acknowledged that the direction is desired.

@Ocramius
Copy link
Member

When not using laminas-http as the HTTP client

Only concern is whether we are breaking any pre-existing behaviour.

I suggest:

  • Documenting the array types (inputs/outputs) that touch the changed code
  • adding tests that prevents regressions on multi-valued headers with laminas-http and without

@@ -47,10 +46,12 @@ public function get($uri, array $headers = [])
}
$response = $this->client->send();

$headers = $response->getHeaders()->toArray() ?? [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im theory this is always an array?

Copy link
Author

@thgs thgs Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh yes, forgot the ?->, will fix it. getHeaders is returning null|Headers

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what you mean now. AbstractMessage (Laminas\Http) handles null. IDE was showing it null|Headers for whatever reason. null is handled on getHeaders.

@@ -155,14 +157,16 @@ private function validateHeaders(array $headers)

/**
* Normalize header names to lowercase.
* Also ensures multi-value headers are represented as a single string, via
* comma concatenation.
*
* @return array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's document this to be an array<string> then? Previously, it was an array<string|array<string>>, so this change looks like a breaking change 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its the opposite I think. Before the normalizeHeaders was expecting array<string, string> and now it can handle array<string, string|array<string>>, as the logic that was in LaminasHttpClientDecorator prepareResponseHeaders now is included in Response normalizeHeaders (see validateHeaders in Response is not accepting multi value (L147 in my branch).

So I thought its not a breaking change as now it can handle what it was handling before and in addition multi-value headers?

@thgs
Copy link
Author

thgs commented Dec 11, 2023

When not using laminas-http as the HTTP client

Only concern is whether we are breaking any pre-existing behaviour.

I suggest:

* Documenting the array types (inputs/outputs) that touch the changed code

* adding tests that prevents regressions on multi-valued headers with laminas-http and without

Yes, will check more myself for BC breaks and do the suggestions soon.

Signed-off-by: thgs <theogpl57@gmail.com>
@thgs thgs force-pushed the move-header-normalisation-into-response branch from dc1f18a to 63614b8 Compare December 18, 2023 01:47
Signed-off-by: thgs <theogpl57@gmail.com>
@thgs thgs force-pushed the move-header-normalisation-into-response branch from 63614b8 to f928367 Compare December 18, 2023 01:48
Signed-off-by: thgs <theogpl57@gmail.com>
Signed-off-by: thgs <theogpl57@gmail.com>
@thgs
Copy link
Author

thgs commented Jan 1, 2024

@Ocramius

Happy new year.

I think I am going to back off from this for now. The benefit is very little and it seems more risky the more I think about it.

Thanks for review!

@thgs thgs closed this Jan 1, 2024
@thgs thgs deleted the move-header-normalisation-into-response branch January 1, 2024 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants